/*
 * L9OutputStream.h
 *
 *  Created on: 2011-10-18
 *      Author: baixp
 */

#ifndef L9OUTPUTSTREAM_H_
#define L9OUTPUTSTREAM_H_
#include <FBase.h>
using namespace Osp::Base;
namespace lib9bada {
class L9OutputStream {
private:
	byte* _buf;
	int _off;
	int _size;
	void autoIncrease(int newsize);
public:
	L9OutputStream();
	L9OutputStream(int capacity);
	virtual ~L9OutputStream();

	/**
	 * 按照由低到高的将int数字写入字节数据，占4个字节
	 * @param arr byte[]
	 * @param off int
	 * @param i int
	 */
	static void writeIntLow(byte* arr, int off, int i) {
		/*
		 //存在问题，不知为啥子
		 arr[off] = (byte) (i & 0x000000FF);
		 arr[off++] = (byte) ((i & 0x0000FF00) >> 8);
		 arr[off++] = (byte) ((i & 0x00FF0000) >> 16);
		 arr[off++] = (byte) ((i & 0xFF000000) >> 24);
		 */
		arr[off] = (byte) (i & 0x000000FF);
		arr[off + 1] = (byte) ((i & 0x0000FF00) >> 8);
		arr[off + 2] = (byte) ((i & 0x00FF0000) >> 16);
		arr[off + 3] = (byte) ((i & 0xFF000000) >> 24);
	}

	/**
	 * 按照由低到高的将int数字写入字节数据，占8个字节
	 * @param arr byte[]
	 * @param off int
	 * @param i long
	 */
	static void writeLongLow(byte* arr, int off, long i) {
		arr[off] = (byte) (i & 0x000000FF);
		arr[off + 1] = (byte) ((i & 0x000000000000FF00L) >> 8);
		arr[off + 2] = (byte) ((i & 0x0000000000FF0000L) >> 16);
		arr[off + 3] = (byte) ((i & 0x00000000FF000000L) >> 24);
		arr[off + 4] = (byte) ((i & 0x000000FF00000000L) >> 32);
		arr[off + 5] = (byte) ((i & 0x0000FF0000000000L) >> 40);
		arr[off + 6] = (byte) ((i & 0x00FF000000000000L) >> 48);
		arr[off + 7] = (byte) ((i & 0xFF00000000000000L) >> 56);
	}

	/**
	 * 按照由低到高的将short数字写入字节数据，占2个字节
	 * @param arr byte[]
	 * @param off int
	 * @param i int
	 */
	static void writeShortLow(byte* arr, int off, short i) {
		arr[off] = (byte) ((i & 0x00FF));
		arr[off + 1] = (byte) ((i & 0xFF00) >> 8);
	}
	void writeBoolean(bool b);
	void writeByte(byte c);
	void writeBytes(const byte* arr, int off, int len);
	void writeShort(short s);
	void writeInt(int s);
	void writeLong(long s);
	void writeString(String pStr);
	byte* getBytesN();
	void Save(const char* file);
	int getOff();
	void setOff(int off);
	int getSize();
	byte* getBuf();
};
}
;
#endif /* L9OUTPUTSTREAM_H_ */
